package com.tadpole.music.view.activity.login;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.multidex.MultiDex;

import com.tadpole.music.R;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.login.SplashIView;
import com.tadpole.music.presenter.login.SplashPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.StatusBarUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.MainActivity;
import com.tadpole.music.view.widget.ZhengCePopup;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import cn.jpush.android.api.JPushInterface;
import static com.tadpole.music.app.MyApplication.mWxApi;
/**
 * 闪屏页面
 * Created By 孙岩
 * on 2020/9/15
 */
public class SplashActivity extends AppCompatActivity implements SplashIView {

    private SplashPresenter splashPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //设置app顶部状态栏颜色
        //将头部状态栏置为透明
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        splashPresenter = new SplashPresenter();
        splashPresenter.attachView(this);
        splashPresenter.getData();
    }

    @Override
    public void taskJump(int pageCode) {
//        switch (pageCode) {
//            case 1:
//                //引导页
//                startActivity(new Intent(this, MainActivity.class));
//                break;
//            case 2:
//                //登录页
//                startActivity(new Intent(this, MainActivity.class));
//                break;
//            case 3:
//                //主页面
//                startActivity(new Intent(this, MainActivity.class));
//                break;
//            default:
//                break;
//        }
//        finish();

        if(SpUtil.getInstance(SplashActivity.this).getString(Constant.FREE_COURSE,"").equals("")){
            showPopWindow();
        }else{
            MultiDex.install(SplashActivity.this);
            //微信
            mWxApi = WXAPIFactory.createWXAPI(SplashActivity.this, Constant.WX_APP_ID, true);
            mWxApi.registerApp(Constant.WX_APP_ID);

            //极光 初始化 JPush
            JPushInterface.init(SplashActivity.this);
            //bugly 蝌蚪
            CrashReport.initCrashReport(getApplicationContext(),"721e9114b0",false);

            SpUtil.getInstance(SplashActivity.this).putString(Constant.FREE_COURSE,"1");

            startActivity(new Intent(SplashActivity.this, MainActivity.class));

            SplashActivity.this.finish();

        }
    }

    private void showPopWindow() {
        final ZhengCePopup popup = new ZhengCePopup(SplashActivity.this);
        popup.setPopupWindowFullScreen(true);
        popup.setDismissWhenTouchOutside(false);
        popup.setDismissWhenTouchOutside(false);
        popup.showPopupWindow();
        popup.setModeListener(new ZhengCePopup.IModeSelection() {
            @Override
            public void getMode(int mode) {
                if (Mutils.isNetworkAvailable()) {
                    if(mode==1){
                        popup.dismiss();
                        SplashActivity.this.finish();
                    }else if(mode==2){
                        MultiDex.install(SplashActivity.this);
                        //微信
                        mWxApi = WXAPIFactory.createWXAPI(SplashActivity.this, Constant.WX_APP_ID, true);
                        mWxApi.registerApp(Constant.WX_APP_ID);

                        //极光 初始化 JPush
                        JPushInterface.init(SplashActivity.this);
                        //bugly 蝌蚪
                        CrashReport.initCrashReport(getApplicationContext(),"721e9114b0",false);



                        SpUtil.getInstance(SplashActivity.this).putString(Constant.FREE_COURSE,"1");
                        popup.dismiss();
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));

                        SplashActivity.this.finish();
                    }
                } else {
                    ToastUtils.show(getResources().getString(R.string.net_work));
                }
            }
        });
    }
    

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void showErr() {

    }

    @Override
    public Context getContext() {
        return null;
    }
}
